package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.dto.LoginDto;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.entity.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-03-23
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    @Override
    public ResponseResult login(LoginDto dto) {
        //1.校验用户提交参数是否为空
        if (dto == null || (StringUtils.isBlank(dto.getPhone()) && StringUtils.isBlank(dto.getPassword()) && StringUtils.isBlank(dto.getEquipmentId()))) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.根据账户名称查询用户信息（得到用户盐salt信息 跟 正确密文）
        if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
            LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUser::getPhone, dto.getPhone());
            ApUser one = this.getOne(queryWrapper);
            if (one == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
            }


            //3.校验用户填写密码是否正确
            //3.1 拿着用户填写明文密码+盐 进行Md5加密 得到密文
            String pwdStr = dto.getPassword() + one.getSalt();
            String userEnterPwdEncoderStr = DigestUtils.md5DigestAsHex(pwdStr.getBytes());

            //3.2 判断上面步骤得到的密文 跟 数据库中正确密文是否一致
            if (!one.getPassword().equals(userEnterPwdEncoderStr)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }


            //4.调用JWT工具类生成令牌
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", one.getId());
            claimMaps.put("userName", one.getName());
            String token = AppJwtUtil.getToken(claimMaps);

            //5.封装响应结果业务数据
            //5.1 数据脱敏
            one.setPassword(null);
            one.setSalt(null);
            one.setPhone(null);

            //5.2 通过Map构建要求响应结果
            HashMap<String, Object> mapResult = new HashMap<>();
            mapResult.put("user", one);
            mapResult.put("token", token);
            return ResponseResult.okResult(mapResult);
        }

        //5. 用户点击了“先看看”使用手机设备进行登录
        if (StringUtils.isNotBlank(dto.getEquipmentId())) {
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", 0);
            claimMaps.put("equipmentId", dto.getEquipmentId());
            String token = AppJwtUtil.getToken(claimMaps);

            HashMap<String, Object> mapResult = new HashMap<>();
            mapResult.put("token", token);
            return ResponseResult.okResult(mapResult);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
}